﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.BaseInfo;

namespace SysManage.Module.BaseInfo
{
    public partial class DataDictionaryEdit : PageBase
    {
        #region ViewPower
        public string id { get { return GetQueryIntValue("id"); } }
        public string dDicType { get { return GetQueryIntValue("DDicType"); } }
        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "DataDictionaryAdd";
            }
        }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {

            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                DataDictionary current = DB.DataDictionarys.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                txtDDicType.Readonly = true;
                txtDDicMember.Readonly = true;
                txtDDicType.Text = current.DDicType;
                txtDDicMember.Text = current.DDicMember;
                txtDDicMemberName.Text = current.DDicMemberName;
                txtUDF1.Text = current.UDF1;
                txtUDF2.Text = current.UDF2;
                txtOrderNo.Text = current.OrderNo.ToString();
            }
            else
            {
                txtDDicType.Text = dDicType;
            }
        }

        #endregion

        #region Events

        private void SaveJobTitle()
        {

            string id = GetQueryIntValue("id");
            if (id != "")
            {
                //更新
                DataDictionary item = DB.DataDictionarys.Find(id);
                if (item == null)
                {
                    Alert.Show("参数错误！", String.Empty);
                    return;
                }
                item.DDicMember = txtDDicMember.Text;
                item.DDicMemberName = txtDDicMemberName.Text;
                item.UDF1 = txtUDF1.Text;
                item.UDF2 = txtUDF2.Text;
                item.OrderNo = Convert.ToInt32(txtOrderNo.Text);
                DB.SaveChanges();
            }
            else
            {
                var item = new DataDictionary();
                item.ID = StringHelper.NewID;
                item.OrderNo = Convert.ToInt32(txtOrderNo.Text);
                item.DDicType = txtDDicType.Text;
                item.DDicMember = txtDDicMember.Text;
                item.DDicMemberName = txtDDicMemberName.Text;
                item.UDF1 = txtUDF1.Text;
                item.UDF2 = txtUDF2.Text;
                DB.DataDictionarys.Add(item);
                DB.SaveChanges();

            }
            
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveJobTitle();

            //Alert.Show("添加成功！", String.Empty, ActiveWindow.GetHidePostBackReference());
            var dd = ActiveWindow.GetHidePostBackReference();
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

    }
}

